import { createRouter, createWebHistory } from 'vue-router'

import CourseListView from '@/views/course/ListView.vue'

import ProjectListView from '@/views/project/ListView.vue'
 
import ResourceListView from '@/views/resource/ListView.vue'


const routes = [
  {
    path: '/',
    component: () => import('../views/layout/LayoutView.vue'),
    children: [
      {
        path: '/',
        redirect: '/course/list'
      },
      {
        path: '/course/list',
        name: 'courseListView',
        component: CourseListView
      },
      {
        path: '/resource/list',
        name: 'resourceListView',
        component: ResourceListView
      },
      {
        path: '/project/list',
        name: 'projectListView',
        component: ProjectListView
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  const toPath = to.path
  if (toPath == '/login') {
    //判断是否已经登录过了
    if (token) {
      next({ path: '/course/list' })
    } else {
      next()
    }
  } else {
    //判断是否已经登录
    if (token) {
      //已经登录了
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
